#include "stdafx.h"
#include "MsgHandle.h"
#include "HandleCode.h"

MsgHandler sMsgHandler;

MsgHandler::~MsgHandler()
{
	UnRegisterMsgFunc();
}
void MsgHandler::RegisterMsgFunc(DWORD dwMsgID, IDelegateHandler* h)
{
	pair<map<DWORD, IDelegateHandler*>::iterator, bool> rst;
	rst = m_mapHandler.insert(make_pair(dwMsgID, h));
	if (rst.second == false)assert(0);
}

void MsgHandler::UnRegisterMsgFunc()
{
	map<DWORD, IDelegateHandler*>::iterator iter;
	for (iter=m_mapHandler.begin(); iter != m_mapHandler.end(); ++iter)
	{
		delete iter->second;
	}
	m_mapHandler.clear();
}
void MsgHandler::ExecuteMessage(LPVOID lp)
{
	map<DWORD, IDelegateHandler*>::iterator iter;
	iter = m_mapHandler.find(((tagMessage*)lp)->dwHDCode);
	if (iter != m_mapHandler.end())
	{
		iter->second->Invoke(lp);
	}
}